package com.boss.salary.controller;

import com.alibaba.excel.support.ExcelTypeEnum;
import com.boss.api.dto.ApiResultDTO;
import com.boss.asset.service.IAssetInfoService;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.service.ICommonService;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.ExcelModel.*;
import com.boss.salary.constant.ApiElementConstant;
import com.boss.salary.entity.*;
import com.boss.salary.parsingExcel.ExcelParsingService;
import com.boss.salary.parsingExcel.impl.ReadSalaryReportExcel;
import com.boss.salary.parsingExcel.impl.WriteSalaryReportExcel;
import com.boss.salary.service.*;
import com.boss.salary.utils.ExcelUtils;
import com.boss.salary.vo.AgencySalaryVO;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ExcelController  extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(ExcelController.class);

    @Resource(name = "personBankExcelParsingServiceImpl")
    private ExcelParsingService excelParsingService;


    @Autowired
    private IPersonOperateService iPersonOperateService;


    @Autowired
    private IPersonInfoService iPersonInfoService;

    @Autowired
    private ISalaryHistoryService iSalaryHistoryService;

    @Autowired
    private ICommonService iCommonService;

    @Autowired
    private IQuerySalaryDataService iQuerySalaryDataService;

    @Autowired
    private IAssetInfoService assetInfoService;

    @Autowired
    private IAgencySalaryService service;

    @Autowired
    private IDepartmentInfoService iDepartmentInfoService;

    @Autowired
    private IAgencyInfoService iAgencyInfoService;

    @Autowired
    private WriteSalaryReportExcel writeSalaryReportExcel;

    @Autowired
    private ReadSalaryReportExcel readSalaryReportExcel;

    @Autowired
    private IOperrateLogsService iOperrateLogsService;


    @PostMapping("/boss_Salary_vue/excel/upload")
    public ApiResultDTO uploadExcel(@RequestHeader("tokenid") String tokenid,
                                    @RequestParam("file")MultipartFile file,
                                    @RequestParam("agencyId") String agencyId) {
        InputStream inputStream=null;
        try {
            UserDTO userDto = getUser(tokenid);
             inputStream = new BufferedInputStream(file.getInputStream());
            ExcelResult<PersonOperate> personAll = excelParsingService.readExcelWithModel(inputStream, PersonBankModel.class, ExcelTypeEnum.valueOf(inputStream),userDto);
            List<PersonOperate> successList = personAll.getSuccessList();
            List<List<String>> errorList = personAll.getErrorList();
            if (errorList.size() > 0) {
                return ApiResultDTO.error(ERROR_RSCODE, errorList.toString());
            } else {
                iPersonOperateService.importExcelBankData(successList,userDto,agencyId);
                return ApiResultDTO.success();
            }
        } catch (IOException e) {
            logger.error("上传失败！", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE,"上传失败！");
        } catch (Exception e) {
            logger.error("上传失败！", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE,e.getMessage());
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    @RequestMapping( value = "/boss_Salary_vue/excel/downloadModel", method = RequestMethod.GET )
    public ApiResultDTO downloadExcel(HttpServletResponse res ){
        String fileName = "人员信息模板";
        OutputStream outputStream=null;
        outputStream=getOutputStream(fileName,res);
        List<PersonBankModel> modelList=new ArrayList<>();
        modelList.add(new PersonBankModel());
        ExcelUtils.writeExcelWithModel(outputStream,modelList,PersonBankModel.class,null);
        return ApiResultDTO.success();
    }

    /**
     * 导出文件时为Writer生成OutputStream
     */
    private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws AppRuntimeException{
        //创建本地文件
        fileName = fileName + ".xls";

        try {
            fileName = new String(fileName.getBytes(), "UTF-8");
            response.addHeader("Content-Disposition", "filename=" + fileName);

            return response.getOutputStream();
        } catch (Exception e) {

            throw new AppRuntimeException("下载失败");
        }
    }

    /**
     * 导出人员变动编辑
     * @return
     */
    @GetMapping("/outPutPersonOperateList")
    public ApiResultDTO outPutPersonOperateList( @RequestHeader("tokenid") String tokenid,
                                                 @RequestParam(value = "agencyId") String agencyId,
                                                 HttpServletResponse response){

        try {
            UserDTO userDTO = getUser(tokenid);

            List<PersonInfo> allAgencyInfo = iPersonInfoService.getAllPersonInfo(agencyId);
            List<Map<String, String>> bank = assetInfoService.getBaseDataSelect(ApiElementConstant.SALBANK, userDTO.getProvince());

            List<PersonBankModel> personBankModels = new ArrayList<>();
            if (allAgencyInfo.size() > 0) {
                for (PersonInfo personInfo : allAgencyInfo) {
                    PersonBankModel personBankModel = new PersonBankModel();
                    personBankModel.setPersonName(personInfo.getPersonName());
                    personBankModel.setIdenNo(personInfo.getIdenNo());
                    if (!StringUtils.isEmpty(personInfo.getSalaCardBank())) {
                        for (Map<String, String> bankCode : bank) {
                            if (personInfo.getSalaCardBank().equals(bankCode.get("code"))) {
                                personBankModel.setSalaCardBank(bankCode.get("name"));
                            }
                        }
                    }
                    personBankModel.setSalaCardNo(personInfo.getSalaCardNo());
                    personBankModels.add(personBankModel);
                }
            }


            String fileName = "人员变动编辑";
            OutputStream outputStream = null;
            outputStream = getOutputStream(fileName, response);
            ExcelUtils.writeExcelWithModel(outputStream, personBankModels, PersonBankModel.class, null);
            return ApiResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResultDTO.error(e.getMessage());
        }
    }

    /**
     * 月结导出
     * @return
     */
    @GetMapping("/outPutSalaryMonthEndList")
    public ApiResultDTO outPutSalaryMonthEndList(@RequestHeader("tokenid") String tokenid,
                                                 @RequestParam(value = "agencyId") String agencyId,
                                                 @RequestParam(value = "flag") String flag,
                                                 HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenIds = iCommonService.getAgencyChildrenId(userDTO, agencyId);
            Map<String, Object> queryParam =new HashMap<>();
            List<SalaryRentalModel>SalaryRentalModels=new ArrayList<>();
            List<SalaryRental> salaryList=null;
            if("0".equals(flag)){
                salaryList=iSalaryHistoryService.getSalaryMonth(queryParam,childrenIds);
            }else if("1".equals(flag)){
                salaryList=iSalaryHistoryService.getSalaryMonthHistory(queryParam,childrenIds);
            }

            if(salaryList.size()>0){
                for (SalaryRental salaryRental : salaryList) {
                    SalaryRentalModel salaryRentalModel=new SalaryRentalModel();
                    if("0".equals(salaryRental.getIsMonthEnd())){
                        salaryRentalModel.setIsMonthEnd("未月结");
                    }else if("1".equals(salaryRental.getIsMonthEnd())){
                        salaryRentalModel.setIsMonthEnd("已月结");
                    }
                    salaryRentalModel.setAgencyCode(salaryRental.getAgencyCode());
                    salaryRentalModel.setAgencyName(salaryRental.getAgencyName());
                    salaryRentalModel.setMonth(salaryRental.getMonth());
                    salaryRentalModel.setYfgzze(salaryRental.getYfgzze());
                    salaryRentalModel.setSfgzze(salaryRental.getSfgzze());
                    SalaryRentalModels.add(salaryRentalModel);
                }
            }
            String fileName = "月结数据";
            OutputStream outputStream=null;
            outputStream=getOutputStream(fileName,response);
            ExcelUtils.writeExcelWithModel(outputStream,SalaryRentalModels,SalaryRentalModel.class,null);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }

    /**
     * 查询工资数据导出
     * @return
     */
    @GetMapping("/outPutPayrollDataList")
    public ApiResultDTO outPutPayrollDataList(@RequestHeader("tokenid") String tokenid,
                                              @RequestParam(value = "choiceOfTime") String choiceOfTime,
                                              @RequestParam(value = "agencyId") String agencyId,
                                              HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenIds = iCommonService.getAgencyChildrenId(userDTO, agencyId);
            Map<String, Object> queryParam =new HashMap<>();
            queryParam.put("choiceOfTime",choiceOfTime);
            List<QuerySalaryData> QuerySalaryDatas = iQuerySalaryDataService.getPayrollData(queryParam,childrenIds);
            List<QuerySalaryDataModel>QuerySalaryDataModels=new ArrayList<>();

            if(QuerySalaryDatas.size()>0){
                for (QuerySalaryData querySalaryData : QuerySalaryDatas) {
                    QuerySalaryDataModel querySalaryDataModel=new QuerySalaryDataModel();
                    querySalaryDataModel.setAgencyName(querySalaryData.getAgencyName());
                    querySalaryDataModel.setRegisterSalary(querySalaryData.getRegisterSalary());
                    querySalaryDataModel.setQuitSalary(querySalaryData.getQuitSalary());
                    querySalaryDataModel.setTotalSalary(querySalaryData.getTotalSalary());
                    QuerySalaryDataModels.add(querySalaryDataModel);
                }
            }
            String fileName = "工资支付数据";
            OutputStream outputStream=null;
            outputStream=getOutputStream(fileName,response);
            ExcelUtils.writeExcelWithModel(outputStream,QuerySalaryDataModels,QuerySalaryDataModel.class,null);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }



    /**
     * 查询工资数据导出批次
     * @return
     */
    @GetMapping("/outPutPayrollDataListDouble")
    public ApiResultDTO outPutPayrollDataListDouble(@RequestHeader("tokenid") String tokenid,
                                              @RequestParam(value = "choiceOfTime") String choiceOfTime,
                                              @RequestParam(value = "agencyId") String agencyId,
                                              HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenIds = iCommonService.getAgencyChildrenId(userDTO, agencyId);
            Map<String, Object> queryParam =new HashMap<>();
            queryParam.put("choiceOfTime",choiceOfTime);
            List<QuerySalaryData> QuerySalaryDatas = iQuerySalaryDataService.getPayrollData(queryParam,childrenIds);
            List<QuerySalaryDataModel>QuerySalaryDataModels=new ArrayList<>();

            if(QuerySalaryDatas.size()>0){
                for (QuerySalaryData querySalaryData : QuerySalaryDatas) {
                    QuerySalaryDataModel querySalaryDataModel=new QuerySalaryDataModel();
                    querySalaryDataModel.setAgencyName(querySalaryData.getAgencyName());
                    querySalaryDataModel.setRegisterSalary(querySalaryData.getRegisterSalary());
                    querySalaryDataModel.setQuitSalary(querySalaryData.getQuitSalary());
                    querySalaryDataModel.setTotalSalary(querySalaryData.getTotalSalary());
                    QuerySalaryDataModels.add(querySalaryDataModel);
                }
            }
            String fileName = "工资支付数据";
            OutputStream outputStream=null;
            outputStream=getOutputStream(fileName,response);
            ExcelUtils.writeExcelWithModel(outputStream,QuerySalaryDataModels,QuerySalaryDataModel.class,null);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }
    /**
     * 单位工资编报导出
     * @return
     */

    public ApiResultDTO outPutSalaryReportList1(@RequestHeader("tokenid") String tokenid,
                                                @RequestParam(value = "agencyId") String agencyId,
                                                @RequestParam(value = "flag") String flag,
                                                @RequestParam Map<String,Object> condition,
                                               HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<SalaryReportModel>SalaryReportModels=new ArrayList<>();
            List<Map<String, Object>> unitSalaryReport = service.getUnitSalaryReport(userDTO,agencyId, flag, condition);
            if(unitSalaryReport.size()>0){
                for (Map<String, Object> map : unitSalaryReport) {
                    SalaryReportModel salaryReportModel=new SalaryReportModel();
                    salaryReportModel.setCurrentMonth(String.valueOf(map.get("CurrentMonth")));
                    if(!"null".equals(String.valueOf(map.get("DeptId")))){
                        DepartmentInfo deptId = iDepartmentInfoService.getDepartmentInfoById((String.valueOf(map.get("DeptId"))));
                        salaryReportModel.setDeptId(deptId.getDeptName());
                    }
                    salaryReportModel.setIdenNo(String.valueOf(map.get("IdenNo")));
                    salaryReportModel.setPersonName(String.valueOf(map.get("PersonName")));
                    if(!"null".equals((String.valueOf(map.get("s1"))))){
                        salaryReportModel.setS1(String.valueOf(map.get("s1")));
                    }else{
                        salaryReportModel.setS1("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s2"))))){
                        salaryReportModel.setS2(String.valueOf(map.get("s2")));
                    }else{
                        salaryReportModel.setS2("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s3"))))){
                        salaryReportModel.setS3(String.valueOf(map.get("s3")));
                    }else{
                        salaryReportModel.setS3("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s4"))))){
                        salaryReportModel.setS4(String.valueOf(map.get("s4")));
                    }else{
                        salaryReportModel.setS4("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s5"))))){
                        salaryReportModel.setS5(String.valueOf(map.get("s5")));
                    }else{
                        salaryReportModel.setS5("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s6"))))){
                        salaryReportModel.setS6(String.valueOf(map.get("s6")));
                    }else{
                        salaryReportModel.setS6("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s7"))))){
                        salaryReportModel.setS7(String.valueOf(map.get("s7")));
                    }else{
                        salaryReportModel.setS7("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s8"))))){
                        salaryReportModel.setS8(String.valueOf(map.get("s8")));
                    }else{
                        salaryReportModel.setS8("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s9"))))){
                        salaryReportModel.setS9(String.valueOf(map.get("s9")));
                    }else{
                        salaryReportModel.setS9("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s10"))))){
                        salaryReportModel.setS10(String.valueOf(map.get("s10")));
                    }else{
                        salaryReportModel.setS10("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s11"))))){
                        salaryReportModel.setS11(String.valueOf(map.get("s11")));
                    }else{
                        salaryReportModel.setS11("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s12"))))){
                        salaryReportModel.setS12(String.valueOf(map.get("s12")));
                    }else{
                        salaryReportModel.setS12("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s13"))))){
                        salaryReportModel.setS13(String.valueOf(map.get("s13")));
                    }else{
                        salaryReportModel.setS13("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s14"))))){
                        salaryReportModel.setS14(String.valueOf(map.get("s14")));
                    }else{
                        salaryReportModel.setS14("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s15"))))){
                        salaryReportModel.setS15(String.valueOf(map.get("s15")));
                    }else{
                        salaryReportModel.setS15("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s16"))))){
                        salaryReportModel.setS16(String.valueOf(map.get("s16")));
                    }else{
                        salaryReportModel.setS16("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s17"))))){
                        salaryReportModel.setS17(String.valueOf(map.get("s17")));
                    }else{
                        salaryReportModel.setS17("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s18"))))){
                        salaryReportModel.setS18(String.valueOf(map.get("s18")));
                    }else{
                        salaryReportModel.setS18("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s19"))))){
                        salaryReportModel.setS19(String.valueOf(map.get("s19")));
                    }else{
                        salaryReportModel.setS19("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s20"))))){
                        salaryReportModel.setS20(String.valueOf(map.get("s20")));
                    }else{
                        salaryReportModel.setS20("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s21"))))){
                        salaryReportModel.setS21(String.valueOf(map.get("s21")));
                    }else{
                        salaryReportModel.setS21("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s22"))))){
                        salaryReportModel.setS22(String.valueOf(map.get("s22")));
                    }else{
                        salaryReportModel.setS22("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s23"))))){
                        salaryReportModel.setS23(String.valueOf(map.get("s23")));
                    }else{
                        salaryReportModel.setS23("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s24"))))){
                        salaryReportModel.setS24(String.valueOf(map.get("s24")));
                    }else{
                        salaryReportModel.setS24("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s25"))))){
                        salaryReportModel.setS25(String.valueOf(map.get("s25")));
                    }else{
                        salaryReportModel.setS25("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s26"))))){
                        salaryReportModel.setS26(String.valueOf(map.get("s26")));
                    }else{
                        salaryReportModel.setS26("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s27"))))){
                        salaryReportModel.setS27(String.valueOf(map.get("s27")));
                    }else{
                        salaryReportModel.setS27("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s28"))))){
                        salaryReportModel.setS28(String.valueOf(map.get("s28")));
                    }else{
                        salaryReportModel.setS28("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s29"))))){
                        salaryReportModel.setS29(String.valueOf(map.get("s29")));
                    }else{
                        salaryReportModel.setS29("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s30"))))){
                        salaryReportModel.setS30(String.valueOf(map.get("s30")));
                    }else{
                        salaryReportModel.setS30("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s31"))))){
                        salaryReportModel.setS31(String.valueOf(map.get("s31")));
                    }else{
                        salaryReportModel.setS31("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s32"))))){
                        salaryReportModel.setS32(String.valueOf(map.get("s32")));
                    }else{
                        salaryReportModel.setS32("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s33"))))){
                        salaryReportModel.setS33(String.valueOf(map.get("s33")));
                    }else{
                        salaryReportModel.setS33("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s34"))))){
                        salaryReportModel.setS34(String.valueOf(map.get("s34")));
                    }else{
                        salaryReportModel.setS34("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s35"))))){
                        salaryReportModel.setS35(String.valueOf(map.get("s35")));
                    }else{
                        salaryReportModel.setS35("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s36"))))){
                        salaryReportModel.setS36(String.valueOf(map.get("s36")));
                    }else{
                        salaryReportModel.setS36("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s37"))))){
                        salaryReportModel.setS37(String.valueOf(map.get("s37")));
                    }else{
                        salaryReportModel.setS37("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s38"))))){
                        salaryReportModel.setS38(String.valueOf(map.get("s38")));
                    }else{
                        salaryReportModel.setS38("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s39"))))){
                        salaryReportModel.setS39(String.valueOf(map.get("s39")));
                    }else{
                        salaryReportModel.setS39("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s40"))))){
                        salaryReportModel.setS40(String.valueOf(map.get("s40")));
                    }else{
                        salaryReportModel.setS40("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s41"))))){
                        salaryReportModel.setS41(String.valueOf(map.get("s41")));
                    }else{
                        salaryReportModel.setS41("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s42"))))){
                        salaryReportModel.setS42(String.valueOf(map.get("s42")));
                    }else{
                        salaryReportModel.setS42("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s43"))))){
                        salaryReportModel.setS43(String.valueOf(map.get("s43")));
                    }else{
                        salaryReportModel.setS43("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s44"))))){
                        salaryReportModel.setS44(String.valueOf(map.get("s44")));
                    }else{
                        salaryReportModel.setS44("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s45"))))){
                        salaryReportModel.setS45(String.valueOf(map.get("s45")));
                    }else{
                        salaryReportModel.setS45("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s46"))))){
                        salaryReportModel.setS46(String.valueOf(map.get("s46")));
                    }else{
                        salaryReportModel.setS46("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s47"))))){
                        salaryReportModel.setS47(String.valueOf(map.get("s47")));
                    }else{
                        salaryReportModel.setS47("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s48"))))){
                        salaryReportModel.setS48(String.valueOf(map.get("s48")));
                    }else{
                        salaryReportModel.setS48("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s49"))))){
                        salaryReportModel.setS49(String.valueOf(map.get("s49")));
                    }else{
                        salaryReportModel.setS49("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s50"))))){
                        salaryReportModel.setS50(String.valueOf(map.get("s50")));
                    }else{
                        salaryReportModel.setS50("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("s51"))))){
                        salaryReportModel.setS51(String.valueOf(map.get("s51")));
                    }else{
                        salaryReportModel.setS51("0");
                    }
                    if(!"null".equals((String.valueOf(map.get("agencyId"))))){
                       // String name = iAgencyInfoService.getNameById(String.valueOf(map.get("agencyId")));
                      //  salaryReportModel.setAgencyId(name);
                    }

                    SalaryReportModels.add(salaryReportModel);
                }
            }

            String fileName = "单位工资编报";
            OutputStream outputStream=null;
            outputStream=getOutputStream(fileName,response);
            ExcelUtils.writeExcelWithModel(outputStream,SalaryReportModels,SalaryReportModel.class,null);

            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }

   /**
     * 单位工资编报导入
     * @return
     */
    @PostMapping("/ImportSalaryReportList")
    public ApiResultDTO ImportSalaryReportList(@RequestHeader("tokenid") String tokenid,
                                               @RequestParam("file") MultipartFile file,
                                               @RequestParam(value = "agencyId") String agencyId){
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("单位工资编报--单位工资编报--导入");
            operrateLogs.setAgencyId(agencyId);
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            List<AgencySalaryVO> exccelColumnName = service.getExccelColumnName(userDTO);
            String province = userDTO.getProvince();
            readSalaryReportExcel.Read(file, province, agencyId, exccelColumnName);
            return ApiResultDTO.success();
        } catch (AppRuntimeException e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导入失败"+e.getMessage());
        }

    }




    /**
     * 单位工资编报导入
     * @return
     */
    @PostMapping("/ImportSalaryReportListDouble")
    public ApiResultDTO ImportSalaryReportListDouble(@RequestHeader("tokenid") String tokenid,
                                               @RequestParam("file") MultipartFile file,
                                               @RequestParam(value = "agencyId") String agencyId){
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("单位工资编报--单位工资编报--导入");
            operrateLogs.setAgencyId(agencyId);
            iOperrateLogsService.saveOperrateLogs(operrateLogs);
            List<AgencySalaryVO> exccelColumnName = service.getExccelColumnName(userDTO);
            String province = userDTO.getProvince();
            readSalaryReportExcel.ReadreadExcelDouble(file, province, agencyId, exccelColumnName);
            return ApiResultDTO.success();
        } catch (AppRuntimeException e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导入失败"+e.getMessage());
        }

    }

    /**
     * 单位工资编报导出
     * @return
     */
    @GetMapping("/outPutSalaryReportList")
    public ApiResultDTO outPutSalaryReportList( @RequestHeader("tokenid") String tokenid,
                                                @RequestParam(value = "agencyId") String agencyId,
                                                @RequestParam(value = "flag") String flag,
                                                @RequestParam Map<String,Object> condition,
                                                HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<Map<String, Object>> unitSalaryReport = service.getUnitSalaryReport(userDTO,agencyId, flag, condition);
            List<AgencySalaryVO> exccelColumnName = service.getExccelColumnName(userDTO);
            String fileName = "单位工资编报";
            OutputStream outputStream = getOutputStream(fileName,response);
            writeSalaryReportExcel.writeExcel03(unitSalaryReport,exccelColumnName,outputStream);
            return ApiResultDTO.success();
        } catch (Exception e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }


    /**
     * 单位工资编报导出
     * @return
     */
    @GetMapping("/outPutSalaryReportListDouble")
    public ApiResultDTO outPutSalaryReportListDouble( @RequestHeader("tokenid") String tokenid,
                                                @RequestParam(value = "agencyId") String agencyId,
                                                @RequestParam(value = "flag") String flag,
                                                @RequestParam Map<String,Object> condition,
                                                HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<Map<String, Object>> unitSalaryReport = service.getUnitSalaryReportDouble(userDTO,agencyId, flag, condition);
            List<AgencySalaryVO> exccelColumnName = service.getExccelColumnName(userDTO);
            String fileName = "单位工资编报";
            OutputStream outputStream = getOutputStream(fileName,response);
            writeSalaryReportExcel.writeExcel03(unitSalaryReport,exccelColumnName,outputStream);
            return ApiResultDTO.success();
        } catch (Exception e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }
}